package com.baidu.service.impl;

import com.baidu.entity.DeptBean;
import com.baidu.entity.PowerBean;
import com.baidu.entity.RoleBean;
import com.baidu.entity.UserBean;
import com.baidu.mapper.UserMapper;
import com.baidu.service.UserService;
import com.baidu.utils.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 创作时间：2019/12/10 14:45
 * 作者：李增强
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;


    @Override
    public List<UserBean> getUserList(UserBean ub, Date sbirthday, Date ebirthday, Page page) {
        Integer deptid = null;
        String uname=null;
        if(ub!=null){
            deptid = ub.getDb().getDeptid();
            uname = ub.getUname();
        }

        return userMapper.getUserList(uname,deptid,sbirthday,ebirthday,page.getStartRecord(),page.getPageSize());
    }

    @Override
    public void saveUser(UserBean userBean) {
        userMapper.saveUser(userBean);
    }

    @Override
    public Integer getUserCount(UserBean ub, Date sbirthday, Date ebirthday){
        Integer deptid = null;
        String uname=null;
        if(ub!=null){
            deptid = ub.getDb().getDeptid();
            uname = ub.getUname();
        }
        return userMapper.getUserCount(uname,deptid,sbirthday,ebirthday);
    }

    @Override
    public UserBean getUserLogin(UserBean userBean) {
        if(userBean!=null){
            List<UserBean> rblist = userMapper.getUserByUname(userBean.getUname());
            if(rblist!=null&&rblist.size()==1){
                if(rblist.get(0).getPwd().equals(userBean.getPwd())){
                    return rblist.get(0);
                }
            }
        }
        return null;
    }

    @Override
    public List<PowerBean> getPowerList(Integer rid) {
        /**
         * 获取rid角色拥有的权限
         * 查出来的是改角色拥有的权限的list，回显只需要权限的id就可以，所以可以直接去中间表，把id查出来就OK啦
         */
        List<PowerBean> rlist = userMapper.getRolePower(rid);
        List<Integer> ids = userMapper.getRolePowerIds(rid);

        /**
         * 获取全部权限列表
         */
        List<PowerBean> list = userMapper.getPowerList();
        if(ids!=null){
            for (Integer id : ids) {
                for (PowerBean pb : list) {
                    if(pb.getId().equals(id)){
                        pb.setChecked(true);
                        break;
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<RoleBean> getRoleList() {
        return userMapper.getRoleList();
    }

    @Override
    public List<DeptBean> getDeptList() {
        return userMapper.getDeptList();
    }

    @Override
    public DeptBean getDeptByDeptid(Integer deptid) {
        return userMapper.getDeptByDeptid(deptid);
    }

    @Override
    public List<PowerBean> getUserPower(Integer id) {
        return userMapper.getUserPower(id);
    }

    @Override
    public void saveRolePower(Integer rid, String ids) {
        userMapper.deleteRolePower(rid);
        if(ids!=null&&ids.length()>=1){
            String[] split = ids.split(",");
            for (String id : split) {
                userMapper.saveRolePower(rid,id);
            }
        }
    }

    @Override
    public UserBean getUserById(UserBean ub) {
        if(ub!=null){
            return userMapper.getUserById(ub.getId());
        }
        return null;
    }

    @Override
    public void saveUserDept(UserBean ub) {
        if(ub!=null){
            userMapper.saveUserDept(ub);
        }

    }

    @Override
    public List<RoleBean> getDeptRoleList(UserBean ub) {
        if(ub!=null&&ub.getDb().getDeptid()!=null){
            return userMapper.getDeptRoleList(ub.getDb().getDeptid());
        }
        return null;
    }

    @Override
    public void saveUserRole(UserBean ub) {
        if(ub!=null){
            userMapper.saveUserRole(ub);
        }
    }

    @Override
    public List<Integer> getDeptRids(Integer deptid) {
        return userMapper.getDeptRids(deptid);
    }

    @Override
    public void saveDeptRole(Integer deptid, Integer[] rids) {
        if(deptid!=null){
            userMapper.updateDeptRole(deptid);
            if(rids!=null&&rids.length>=1){
                for (Integer rid : rids) {
                    userMapper.saveDeptRole(rid,deptid);
                }
            }
        }
    }

    @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);
    }

    @Override
    public void updateUser(UserBean ub) {
        if(ub!=null){
            userMapper.updateUser(ub);
        }
    }
}
